package com.sgcc.ln.pmos.common.http;

import com.blankj.utilcode.util.ActivityUtils;
import com.google.gson.Gson;
import com.sgcc.ln.pmos.common.utils.UrlUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.builder.PostStringBuilder;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;

public class HttpRequestUtils implements HttpRequestInterf {

    private static HttpRequestUtils INSTANCE = new HttpRequestUtils();

    private HttpRequestUtils() {
    }

    public static synchronized HttpRequestUtils getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new HttpRequestUtils();
        }
        return INSTANCE;
    }

    public void requestArrayMap(ArrayList<HashMap<String, Object>> params, String url, Callback callback) {
        requestArray(new Gson().toJson(params), url, callback);
    }

    public void requestArrayString(ArrayList<String> params, String url, Callback callback) {
        requestArray(new Gson().toJson(params), url, callback);
    }

    public void requestArray(String params, String url, Callback callback) {
        OkHttpUtils.postString().tag(ActivityUtils.getTopActivity()).url(url).mediaType(MediaType.parse("application/json; charset=utf-8")).content(params).build().execute(callback);
    }

    public void requestWithDiffHeaders(Map<String, Object> params, String url, Callback callback, Map<String, String> headers) {
        this.requestWithDiffHeaders(params, url, ActivityUtils.getTopActivity(), callback, headers);
    }

    public void requestWithDiffHeaders(Map<String, Object> params, String url, Object tag, Callback callback, Map<String, String> headers) {
        if (params == null) params = new HashMap<>();
        if (!url.startsWith("http")) url = "http://" + url;
        publicRequest(new Gson().toJson(params), url, tag, callback, headers);
    }

    public void request(Map<String, Object> params, String url, Callback callback) {
        this.request(params, url, ActivityUtils.getTopActivity(), callback);
    }

    public void request(Map<String, Object> params, String url, Object tag, Callback callback) {
        requestWithDiffHeaders(params, url, tag, callback, UrlUtils.getHeaderByUrl(url));
    }

    public void requestFile(Map<String, String> params, String keyname, String url, File file, Callback callback) {
        try {
            OkHttpUtils.post().tag(ActivityUtils.getTopActivity()).params(params).url(url).addFile(keyname, URLEncoder.encode(file.getName(), "UTF-8"), file).build().execute(callback);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    public void requestFileWithHeader(Map<String, String> params, Map<String, String> header, String keyname, String url, File file, Callback callback) {
        PostFormBuilder builder = OkHttpUtils.post().tag(ActivityUtils.getTopActivity()).params(params);
        if (header != null) {
            for (String s : header.keySet()) {
                builder.addHeader(s, header.get(s));
            }
        }
        builder.url(url).addFile(keyname, file.getName(), file).build().execute(callback);
    }

    public void requestGet(Map<String, String> params, String url, Callback callback) {
        OkHttpUtils.get().tag(ActivityUtils.getTopActivity()).url(url).params(params).build().execute(callback);
    }


    /**
     * 公共的String形式提交
     *
     * @param content
     * @param url
     * @param callback
     */
    public void publicRequest(String content, String url, Callback callback, Map<String, String> headers) {
        this.publicRequest(content, url, ActivityUtils.getTopActivity(), callback, headers);
    }

    public void publicRequest(String content, String url, Object tag, Callback callback, Map<String, String> headers) {
        PostStringBuilder builder = OkHttpUtils.postString().tag(tag).url(url);
        if (headers != null) {
            for (String s : headers.keySet()) {
                builder.addHeader(s, headers.get(s));
            }
        }
        builder.mediaType(MediaType.parse("application/json; charset=utf-8")).content(content).build().execute(callback);
    }

    @Override
    public void cancel() {
        try {
            finishInstance();
        } catch (Exception e) {
        }
    }

    public void cancel(Object tag) {
        try {
            OkHttpUtils.getInstance().cancelTag(tag);
        } catch (Exception e) {
        }
    }

    public static void finishInstance() {
        INSTANCE = null;
    }


}